Skip to content

Conversation

EFanZh
Copy link
Contributor

@EFanZh EFanZh commented May 21, 2025

This PR makes Rc::deref zero-cost by changing the internal pointer to point directly to the value instead of to the allocation.

This PR is split from #132553, which will also make Arc::deref zero-cost.

Review status:

  • 8d8878a Add RefCounts and RcLayout types
  • abe9c27 Add allocation functions
  • cfa6ea8 Add RefCounter trait
  • 77755f0 Add RawWeak type
  • 7fce1c4 Add RawWeak methods for sized values
  • adddf22 Add RawWeak methods for slice values
  • 99522fc Implement necessary traits for RawWeak
  • 393e377 Add RawRc type
  • 6cb537c Add RawRc methods for sized values
  • 6b47771 Add RawRc methods for MaybeUninit<T> values
  • 9184466 Add RawRc methods for slice values
  • 33881a5 Add RawRc methods for dyn Any type
  • 17e84c6 Implement necessary traits for RawRc
  • 20e0aba Add RawUniqueRc type
  • 82525d0 Add RawUniqueRc methods for sized values
  • 8bc9dea Implement necessary traits for RawUniqueRc
  • cee99e8 Implement alloc::rc::{Rc,Weak,UniqueRc} with alloc::raw_rc types
  • 2676c63 (HEAD -> zero-cost-rc-deref) Add LLVM codegen tests for new Rc implementation

@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 21, 2025
@rust-log-analyzer

This comment has been minimized.

@EFanZh EFanZh force-pushed the zero-cost-rc-deref branch from df34f84 to d3a7429 Compare May 24, 2025 05:01
@rust-log-analyzer

This comment has been minimized.

@EFanZh EFanZh force-pushed the zero-cost-rc-deref branch 2 times, most recently from bc84ec6 to 19fb34b Compare May 24, 2025 09:00
@EFanZh EFanZh marked this pull request as ready for review May 24, 2025 10:22
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 24, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 24, 2025

The Miri subtree was changed

cc @rust-lang/miri

@EFanZh EFanZh force-pushed the zero-cost-rc-deref branch from 19fb34b to f5245ba Compare May 26, 2025 15:02
@oli-obk
Copy link
Contributor

oli-obk commented May 26, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 26, 2025
@bors
Copy link
Collaborator

bors commented May 26, 2025

⌛ Trying commit f5245ba with merge 8ef4a25...

bors added a commit that referenced this pull request May 26, 2025
Make `Rc<T>::deref` zero-cost

This PR makes `Rc::deref` zero-cost by changing the internal pointer so that it points to the value directly instead of the allocation.

This is split out from #132553, which will also make `Arc::deref` zero-cost.
@bors
Copy link
Collaborator

bors commented May 26, 2025

☀️ Try build successful - checks-actions
Build commit: 8ef4a25 (8ef4a25b05973cfbd577205c507a891d07f0ae5f)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (8ef4a25): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
0.5% [0.2%, 0.7%] 4
Regressions ❌
(secondary)
1.1% [0.2%, 2.5%] 9
Improvements ✅
(primary)
-1.0% [-2.0%, -0.5%] 7
Improvements ✅
(secondary)
-1.3% [-2.0%, -0.1%] 4
All ❌✅ (primary) -0.5% [-2.0%, 0.7%] 11

Max RSS (memory usage)

Results (primary -0.3%, secondary -0.0%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
8.4% [8.4%, 8.4%] 1
Regressions ❌
(secondary)
3.1% [2.0%, 4.8%] 6
Improvements ✅
(primary)
-4.6% [-7.1%, -2.1%] 2
Improvements ✅
(secondary)
-4.7% [-6.4%, -2.9%] 4
All ❌✅ (primary) -0.3% [-7.1%, 8.4%] 3

Cycles

Results (primary -0.5%, secondary -1.9%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.9% [0.9%, 0.9%] 1
Regressions ❌
(secondary)
2.1% [2.1%, 2.1%] 2
Improvements ✅
(primary)
-1.9% [-1.9%, -1.9%] 1
Improvements ✅
(secondary)
-3.9% [-9.3%, -1.5%] 4
All ❌✅ (primary) -0.5% [-1.9%, 0.9%] 2

Binary size

Results (primary 0.2%, secondary 1.6%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.3% [0.0%, 1.8%] 29
Regressions ❌
(secondary)
1.8% [0.0%, 6.4%] 9
Improvements ✅
(primary)
-0.3% [-0.6%, -0.1%] 5
Improvements ✅
(secondary)
-0.4% [-0.4%, -0.4%] 1
All ❌✅ (primary) 0.2% [-0.6%, 1.8%] 34

Bootstrap: 775.728s -> 776.531s (0.10%)
Artifact size: 366.25 MiB -> 366.33 MiB (0.02%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels May 26, 2025
@apiraino
Copy link
Contributor

Would anyone comment the perf. run? cc @EFanZh thanks!

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 31, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 31, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@EFanZh EFanZh force-pushed the zero-cost-rc-deref branch from 62c8b9c to eccd4c2 Compare September 9, 2025 16:42
@rust-log-analyzer

This comment has been minimized.

@Dylan-DPC Dylan-DPC added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 20, 2025
@bors
Copy link
Collaborator

bors commented Sep 30, 2025

☔ The latest upstream changes (presumably #147197) made this pull request unmergeable. Please resolve the merge conflicts.

@EFanZh EFanZh force-pushed the zero-cost-rc-deref branch from 1266321 to 616991d Compare October 1, 2025 14:22
@rustbot

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Oct 4, 2025

☔ The latest upstream changes (presumably #147340) made this pull request unmergeable. Please resolve the merge conflicts.

@EFanZh EFanZh force-pushed the zero-cost-rc-deref branch from 616991d to 31d5740 Compare October 5, 2025 07:15
@rustbot
Copy link
Collaborator

rustbot commented Oct 5, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Comment on lines +27 to +29
//! | Padding | `size_of::<RefCounts>().next_multiple_of(align_of::<T>()) - size_of::<RefCounts>()` |
//! | `RefCounts` | `size_of::<RefCounts>()` |
//! | `T` | `size_of::<T>()` |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this puts the data after the refcount. Would it later still be possible to change it to data, padding and then the refcounts? That'd make it possible to convert a Rc<[T]> to Vec<T> without memcpy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@the8472: I think there are several problems:

  • The current design employs the strategy from [DRAFT] Rc: allow deduping both deref and clone across types #133061, where using a fixed offset of the reference counter could potentially allowing sharing the same clone implementation across different Rc types.
  • Not using a fixed offset makes the debug pretty printer more difficult to write.
  • The buffer allocated with Rc has alignment of align_of::<RefCounts>().max(align_of::<T>()), which might be different with align_of::<T>(). Since Vec<T> will deallocate using a Layout with align_of::<T>(), and deallocating requires the alignment being exactly the same as the one that was used to do allocation according to https://doc.rust-lang.org/stable/std/alloc/trait.Allocator.html#memory-fitting, Vec<T> might not be able to deallocate the memory allocated by Rc<[T]>, at least not with the current Vec implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.